<?php
/*
Plugin Name: Map Maker
Plugin URI: https://code.google.com/p/wp-map-maker/
Description: Create a map on the post
Version: 0.0.1
Author: Charlie Chang
Author URI: http://icharlie.github.io/
License: The MIT License (MIT)
*/


// register_activation_hook(__FILE__, 'map_maker_install');
// function map_maker_install()
// {
//     // add_option('map_maker_otpions', array(
//     //     'mapID' => ''));
// }

// register_deactivation_hook(__FILE__, 'map_maker_deactivate');
// function map_maker_deactivate()
// {
//     delete_option('map_maker_otpions');
// }


// add script into page
add_action('wp_enqueue_scripts', 'inject_map_maker_script');

function inject_map_maker_script()
{
    wp_enqueue_script('mapbox', plugins_url('js/mapbox.js', __FILE__), false);
    wp_register_script('wp-map-maker-js', plugins_url('js/wp-map-maker.js', __FILE__), array(), null, true);
    wp_enqueue_script('wp-map-maker-js');

    $wp_map_maker_plugin_options = array('img_url' => plugins_url('images', __FILE__), 'mapID' => get_option('map_maker_mapID', ''));
    wp_localize_script('wp-map-maker-js', 'wp_map_maker_plugin_options', $wp_map_maker_plugin_options);
}

// add style into page
add_action('wp_enqueue_scripts', 'inject_map_maker_style');

function inject_map_maker_style()
{
    wp_enqueue_style('mapbox', plugins_url('css/mapbox.css', __FILE__), false);
    wp_enqueue_style('wp-map-maker', plugins_url('css/wp-map-maker.css', __FILE__), false);
}

// add map maker into setting page
add_action('admin_menu', 'create_map_maker_menu');
function create_map_maker_menu()
{
    $map_maker_menu = add_options_page(
        'Map Make Settings',
        'Map Make',
        'manage_options',
        'map_maker',
        'map_maker_settings_page'
    );
    // add_action('add_init', 'regiester_map_maker_settings');
    add_action('admin_print_styles-'.$map_maker_menu, 'inject_mapy_maker_admin_css_custom_page');
}

function regiester_map_maker_settings()
{
    register_setting('map-maker-options-group', 'map_maker_mapID');
}

function map_maker_settings_page()
{
    if (isset($_REQUEST['map_maker_mapID'])) {
        update_option('map_maker_mapID', $_REQUEST['map_maker_mapID']);
    }
    include(plugin_dir_path(__FILE__).'view/settings.php');
}

// add style into map maker setting page
// add_action('admin_enqueue_scripts', 'inject_mapy_maker_setting_page_script_and_style');
function inject_mapy_maker_admin_css_custom_page()
{
    wp_register_style('map_maker_plugin_page_css', plugins_url('css/map-maker-admin.css', __FILE__));
    wp_enqueue_style('map_maker_plugin_page_css');
}
